/**
 * @FileName: HttpClient.java
 * @author wangshangyu
 * @date 2014-9-18 下午05:39:53
 * @version 1.0
 */
package util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
 

 /**
  * @description  http请求类
  * @author
  * @date 2015-8-23下午6:47:52
  * @version 1.0
  */
public class HttpClients {

	public HttpClients() {
	}

 

	/**
	 * @param url 请求地址
	 * @return
	 */
	public static String httpGet(String url) {
		String result = "";
		try {
			HttpURLConnection hcon = (HttpURLConnection) new URL(url).openConnection();
			hcon.setDoOutput(true);
			hcon.setRequestMethod("GET");
			hcon.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			hcon.setRequestProperty("user-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			hcon.connect();

			InputStream is = hcon.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = null;
			while ((line = br.readLine()) != null) {
				result += line;
			}
			is.close();
			// 断开连接
			hcon.disconnect();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * @param url
	 * @param param
	 * @return
	 */
	public static String httpPost(String url, String param) {
		String result = "";
		try {
			java.net.HttpURLConnection hcon = (java.net.HttpURLConnection) new java.net.URL(url).openConnection();
			hcon.setDoInput(true);
			hcon.setDoOutput(true);
			hcon.setUseCaches(false);
			hcon.setRequestMethod("POST");
			hcon.setConnectTimeout(3000);
			hcon.setRequestProperty("Accept", "*/*");
			hcon.setRequestProperty("Connection", "Keep-Alive");
			hcon.setRequestProperty("Content-Type", "application/json");
			//hcon.setRequestProperty("Content-type", "application/x-java-serialized-object");
			//hcon.setRequestProperty("Content-Type", "application/html;charset=UTF-8");
			hcon.setRequestProperty("user-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			hcon.connect();

			java.io.PrintWriter out = new java.io.PrintWriter(new java.io.OutputStreamWriter(hcon.getOutputStream(), "UTF-8"));
			out.write(param);
			out.flush();
			out.close();

			java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(hcon.getInputStream(), "UTF-8"));
			// 读取服务器对于此次http请求的返回信息
			String line = null;
			while ((line = in.readLine()) != null) {
				result += line;
			}
			in.close();
			// 断开连接
			hcon.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	 

	public static void main(String[] args) {
/*		GsonBuilder builder = new GsonBuilder();
		Gson gson = builder.create();
		//City c=new City("南京", "江宁区","江宁");
		String url = " http://211.103.0.238:9013/izsyw/fault/resource/getEnodeb.do?regionName=南京&cityName=江宁区&resName=江宁";
		String param1="regionName=南京&cityName=江宁区&resName=江宁";
 
		
//		String param = "{\"ret_count\":1,\"parse_result\":[{\"app_id\":400401,\"app_name\":\"TRAVEL.TRAIN.STATIONTOSTATION\",\"match_score\":0,\"negation\":0,\"text\":\"\",\"semantic\":{\"depart_date\":\"2014-10-17\",\"depart_time\":\"全天\",\"end_city\":\"高碑店\",\"start_city\":\"北京\",\"train_type\":\"全部列车\",\"result_type\":1}}],\"user_info\":{\"userid\":\"0013398181\"},\"location_info\":{\"province\":\"\",\"city\":\"\",\"address\":\"\",\"nearest_poi_name\":\"\",\"user_latitude\":\"\",\"user_longitude\":\"\"}}";
	 	String param =gson.toJson(c);
		String res = httpPost(url,param1);
		String [] array=res.split(",");
		String []s=array[2].split(":");
		System.out.println(s[1]);
		String res1="{\"returnCode\":\"0\",\"description\":\"OK\",\"result\":\"1\"}";
		//JSONArray json = JSONArray.fromObject(res);
       //  HttpResult hr = gson.fromJson(res, HttpResult.class); 
		System.out.println(res);*/
	}
	
	
	
	
}
